﻿using Kinlo.Entitys;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using UIWindows.Models;
using UIWindows.Models.Homes;
using UIWindows.Models.Interact;
using UIWindows.Models.MES;
using UIWindows.Services.Handle;
using UIWindows.UserControls;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace UIWindows.GlobalStatics
{
    /// <summary>
    /// 全局临时变量----作为各个地方过度使用的不保存
    /// </summary>
    [Attributes.Singleton]
    public class TemporaryStatic : Domains.ViewModelBase
    {
        #region 单项通知

        private bool _isRunStatus;
        /// <summary>
        /// 设备是否在运行
        /// </summary>
        public bool IsRunStatus
        {
            get { return _isRunStatus; }
            set { SetProperty(ref _isRunStatus, value); }
        }

        public string Mes_User { get; set; }
        public bool Is_Mes_Login { get;set; }
        public List<ProductionData> EntityCache { get; set; }
        private Dictionary<string, int> _devicestatus;
        public Dictionary<string, int> DeviceStatus { get => _devicestatus; }
        #endregion

        public AsyncObservableCollection<FunPointCheckModel> FunCheckList { get; set; }

        #region 双向绑定
        private Visibility _isExcision = Visibility.Collapsed;

        public Visibility IsExcision
        {
            get { return _isExcision; }
            set { SetProperty(ref _isExcision, value); }
        }
        private bool _notificationRefresh;
        /// <summary>
        /// 通知刷新
        /// </summary>
        public bool NotificationRefresh
        {
            get { return _notificationRefresh; }
            set { SetProperty(ref _notificationRefresh, value); }
        }
        #endregion
        /// <summary>
        /// 点检次数
        /// </summary>
        public int SpotCheckConut { get; set; }
        /// <summary>
        /// 生产出站的随机一个电池
        /// </summary>
        public long FindBySnowflakeId { get; set; }
        public List<PLCAlarmModel> PLCAlarmList { get; set; }
        public ObservableCollection<LinkStatusModel> LinkList { get; set; }
        public ObservableCollection<PLCResectionModel> ExcisionList { get; set; }
        public DateTime Status_StartTime { get; set; }
        public TemporaryStatic()
        {
            FunCheckList = new AsyncObservableCollection<FunPointCheckModel>();
            var objProper = typeof(FunctionSpotCheckModel).GetProperties();
            for (int i = 0; i < objProper.Length; i++)
            {
                FunCheckList.Add(new FunPointCheckModel()
                {
                    Index = i + 1,
                    Status = "未点检",
                    PointCheckTime = DateTime.Now,
                    Function = objProper[i].Name.Split('_')[1],
                    PointCheckNumber = 0,
                });
            }
            EntityCache = new List<ProductionData>();
            _devicestatus = new Dictionary<string, int>();
            LinkList = new ObservableCollection<LinkStatusModel>()
            {
                //new LinkStatusModel(){ 
                //    ServiceName = "MES状态",
                //    Status = false,
                //},
                new LinkStatusModel(){ 
                    ServiceName = "上位机状态",
                    Status = false,
                },
                new LinkStatusModel()
                {
                    ServiceName = "设备状态",
                    Status = true,
                },
            };
            ExcisionList = new ObservableCollection<PLCResectionModel>();
            PLCAlarmList = new List<PLCAlarmModel>();
            if (!Directory.Exists(".\\PLCAlarm")) { Directory.CreateDirectory(".\\PLCAlarm"); }
            if (File.Exists(".\\PLCAlarm\\Alarm"))
            {
                using (FileStream fs = new FileStream(".\\PLCAlarm\\Alarm", FileMode.Open, FileAccess.Read))
                {
                    HSSFWorkbook workbook = new HSSFWorkbook(fs);
                    ISheet sheet = workbook.GetSheetAt(0); 
                    for (int i = 0; i <= sheet.LastRowNum; i++)
                    {
                        IRow row = sheet.GetRow(i);
                        if (row != null && row.Cells.Count >= 2)
                        {
                            if (row.Cells[0].CellType == CellType.String && !string.IsNullOrEmpty(row.Cells[0].StringCellValue) && row.Cells[0].StringCellValue == "exit")
                            {
                                return;
                            }
                            PLCAlarmList.Add(new PLCAlarmModel()
                            {
                                Code = GetValueType(row.Cells[0])?.ToString(),
                                Message = GetValueType(row.Cells[1])?.ToString(),
                            });
                        }
                        else
                        {
                            PLCAlarmList.Add(new PLCAlarmModel() 
                            { 
                                Code = "",
                                Message = "未描述",
                            });
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 获取单元格类型
        /// </summary>
        /// <param name="cell">目标单元格</param>
        /// <returns></returns>
        private static object GetValueType(ICell cell)
        {
            if (cell == null)
                return null;
            switch (cell.CellType)
            {
                case CellType.Blank:
                    return null;
                case CellType.Boolean:
                    return cell.BooleanCellValue;
                case CellType.Numeric:
                    return cell.NumericCellValue;
                case CellType.String:
                    return cell.StringCellValue;
                case CellType.Error:
                    return cell.ErrorCellValue;
                case CellType.Formula:
                default:
                    return "=" + cell.CellFormula;
            }
        }
    }
}
